package me.like.diandian.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.text.TextUtils;

import org.apache.commons.codec.binary.Base64;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.StreamCorruptedException;

import me.like.diandian.bean.StartImgBean;
import me.like.diandian.bean.UserLoginBean;

/**
 * 缓存工具类
 */
public class SPUtils {

    public static final String IS_SPLASH = "isSplash"; // 是否跳过了引导页
    public static final String IS_FRIST_HOME = "is_frist_home"; // 是否第一次进首页
    public static final String IS_ONLE = "isOnle"; // 是否是线上线下
    public static final String IS_CESHI_ONLE = "isCeshiOnle"; // 是否是默认测试环境还是自己配置的环境
    public static final String PREFERNCE_FILE_NAME = "xiaorong"; // 缓存文件名
    public static final String USER_LOGIN = "userLogin";
    public static final String START_IMG = "startImg";
    public static final String TOKEN = "token";
    /**
     * 认证界面跳转到个人中心还是首页
     * 1.MainActivity,2.PeopleCenterActivity
     */
    public static final String RENZHENG_INTENT = "RENZHENG_INTENT";


    /**
     * 第一次进入高级认证弹出
     */
    public static final String ISFIRST_HIGH = "ISFIRST_HIGH";


    /**
     * 可以自己配置后台的baseUrl
     */
    public static final String BASE_URL = "baseUrl";


    public static final String IS_SHOW_TIE = "isshowtie"; // 是否已经显示了提额提示图片


    public static void setBaseUrl(Context context, String baseUrl) {
        saveObj(context, baseUrl, BASE_URL);
    }

    public static String getBaseUrl(Context context) {
        return (String) readObj(context, BASE_URL);
    }


    public static void setOnLine(Context context, boolean isOnline) {
        SharedPreferences prefe = context.getSharedPreferences(IS_ONLE, 0);
        Editor editor = prefe.edit();
        editor.putBoolean("changeOnline", isOnline);
        editor.commit();
    }

    public static boolean isOnLine(Context context) {
        SharedPreferences prefe = context.getSharedPreferences(IS_ONLE, 0);
        boolean b = prefe.getBoolean("changeOnline", true);
        return b;
    }


    public static void setCeshiOnLine(Context context, boolean isOnline) {
        SharedPreferences prefe = context.getSharedPreferences(IS_CESHI_ONLE, 0);
        Editor editor = prefe.edit();
        editor.putBoolean("changeOnline", isOnline);
        editor.commit();
    }

    public static boolean isCeshiOnLine(Context context) {
        SharedPreferences prefe = context.getSharedPreferences(IS_CESHI_ONLE, 0);
        boolean b = prefe.getBoolean("changeOnline", false);
        return b;
    }


    public static void setFisrt(Context context) {
        SharedPreferences prefe = context.getSharedPreferences(IS_SPLASH, 0);
        Editor editor = prefe.edit();
        editor.putBoolean("isSplash", false);
        editor.commit();
    }

    public static void setFisrtHome(Context context) {
        SharedPreferences prefe = context.getSharedPreferences(IS_FRIST_HOME, 0);
        Editor editor = prefe.edit();
        editor.putBoolean("isSplash", false);
        editor.commit();
    }

    public static void setStartImg(Context context, StartImgBean bean) {
        saveObj(context, bean, START_IMG);
    }

    public static StartImgBean getStartImg(Context context) {
        StartImgBean bean = (StartImgBean) readObj(context, START_IMG);
        return bean;
    }

    public static void setUserLogin(Context context, UserLoginBean bean) {
        saveObj(context, bean, USER_LOGIN);
        setToken(context,bean.getToken());
    }

    public static UserLoginBean getUserLogin(Context context) {
        UserLoginBean bean = (UserLoginBean) readObj(context, USER_LOGIN);
        return bean;
    }

    public static String getToken(Context context) {
        SharedPreferences prefe = context.getSharedPreferences(TOKEN, 0);
        String string = prefe.getString(TOKEN, "");
        return string;
    }

    public static boolean isFisrtHome(Context context) {
        SharedPreferences prefe = context.getSharedPreferences(IS_FRIST_HOME, 0);
        boolean b = prefe.getBoolean("isSplash", true);
        return b;
    }

    public static void setToken(Context context, String token) {
        SharedPreferences prefe = context.getSharedPreferences(TOKEN, 0);
         prefe.edit().putString(TOKEN,token).commit();

    }

    public static boolean isFirst(Context context) {
        SharedPreferences prefe = context.getSharedPreferences(IS_SPLASH, 0);
        boolean b = prefe.getBoolean("isSplash", true);
        return b;
    }


    public static void clearByKey2(String key, Context context) {
        SharedPreferences prefe = context.getSharedPreferences(key, 0);
        Editor editor = prefe.edit();
        editor.putString(key, "");
        editor.commit();

    }


    /**
     * 读取一个对象
     *
     * @param context
     * @return
     */
    public static Object readObj(Context context, String key) {
        Object obj = null;
        SharedPreferences prefe = context.getSharedPreferences(PREFERNCE_FILE_NAME, 0);
        String replysBase64 = prefe.getString(key, "");
        if (TextUtils.isEmpty(replysBase64)) {
            return obj;
        }
        // 读取字节
        byte[] base64 = Base64.decodeBase64(replysBase64.getBytes());
        // 封装到字节读取流
        ByteArrayInputStream bais = new ByteArrayInputStream(base64);
        try {
            // 封装到对象读取流
            ObjectInputStream ois = new ObjectInputStream(bais);
            try {
                // 读取对象
                obj = ois.readObject();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        } catch (StreamCorruptedException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return obj;
    }

    /**
     * 存储一个对象
     *
     * @param context
     * @param key
     */
    public static <T> void saveObj(Context context, T obj, String key) {
        T _obj = obj;

        SharedPreferences prefe = context.getSharedPreferences(PREFERNCE_FILE_NAME, 0);
        // 创建字节输出流
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {
            // 创建对象输出流,封装字节流
            ObjectOutputStream oos = new ObjectOutputStream(baos);
            // 将对象写入字节流
            oos.writeObject(_obj);
            // 将字节流编码成base64的字符串
            String list_base64 = new String(Base64.encodeBase64(baos.toByteArray()));
            Editor editor = prefe.edit();
            editor.putString(key, list_base64);
            editor.commit();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
